<template>
    <Dialog :title="title" :dialogVisible="data.visible" @close="close" :width="800" @confirm="confirm">
        <div class="form-content">
            <el-form :model="data.form" :rules="data.rules" label-width="100px" ref="formRef" label-position="top">
                <el-row :gutter="20">
                    <el-col :span="12">
                        <el-form-item label="用户名" prop="username">
                            <el-input v-model="data.form.username" placeholder="请输入用户名" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="密码" prop="password" v-if="data.type === 'add'">
                            <el-input v-model="data.form.password" type="password" show-password placeholder="请输入密码" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="12">
                        <el-form-item label="邮箱" prop="email">
                            <el-input v-model="data.form.email" placeholder="请输入邮箱" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="12">
                        <el-form-item label="手机号" prop="phone">
                            <el-input v-model="data.form.phone" placeholder="请输入手机号" />
                        </el-form-item>
                    </el-col>

                    <el-col :span="12">
                        <el-form-item label="昵称" prop="full_name">
                            <el-input v-model="data.form.full_name" placeholder="请输入昵称" />
                        </el-form-item>
                    </el-col>

                </el-row>
            </el-form>
        </div>

    </Dialog>
</template>

<script setup>
import Dialog from '@/components/dialog/index.vue'
import { useMessage } from '@/hooks/useMessage'
import { userGet, userCreate, userUpdate } from '@/api/user'

const message = useMessage()


const emit = defineEmits(['success'])

const data = reactive({
    form: {
        username: '',
        password: '',
        email: '',
        phone: '',
        full_name: '',
        dept_id: '',
    },
    visible: false,
    type: 'add',
    rules: {
        username: [
            { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { pattern: /^[a-zA-Z0-9]{6,16}$/, message: '密码长度为6-16位', trigger: 'blur' }
        ],
        email: [
            { required: true, message: '请输入邮箱', trigger: 'blur' },
            { pattern: /@/, message: '请输入正确的邮箱', trigger: 'blur' }
        ],
        phone: [
            { required: true, message: '请输入手机号', trigger: 'blur' },
            { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        full_name: [
            { required: true, message: '请输入昵称', trigger: 'blur' }
        ]
    },
    row: null
})

const title = computed(() => {
    return data.type === 'add' ? '新增用户' : '编辑用户'
})

const open = (type, row) => {
    data.visible = true
    data.type = type
    data.row = row
    if (row) {
        getUserInfo(row.id)
    }
}

const getUserInfo = async(id) => {
    const res = await userGet({ id })
    if (res.code == 200) {
        data.form = res.data
    }
}

const close = () => {
    data.visible = false

    data.form = {
        username: '',
        password: '',
        email: '',
        phone: '',
    }
}

const formRef = ref(null)
const confirm = async () => {
    const valid = await formRef.value.validate()

    if (valid) {
        const api = data.type === 'add' ? userCreate : userUpdate
        const res = await api(data.form)
        if (res.code == 200) {
            message.alertMessage('操作成功', 'success')
            close()
            emit('success')
        } else {
            message.alertMessage(res.msg, 'error')
        }
    } else {
        message.alertMessage('请填写完整', 'error')
    }
}


defineExpose({
    open
})
</script>

<style scoped lang="scss">
.form-content {
    width: 100%;
    height: 100%;
    padding: 20px;
}
</style>